package com.xmh.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

	/**
	 * 返回两个时间作为开头和结尾的时间集合
	 * 
	 * @param minDate
	 * @param maxDate
	 * @return
	 */
	public static final List<Date> getDates(Date minDate, Date maxDate) {
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(minDate);

		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(maxDate);

		List<Date> dates = new ArrayList<Date>();

		dates.add(minDate);

		while (startCalendar.before(endCalendar)) {
			startCalendar.add(Calendar.MONTH, 1);

			dates.add(startCalendar.getTime());
		}

		dates.remove(dates.size() - 1);

		startCalendar = null;
		endCalendar = null;

		return dates;
	}

	/**
	 * 判断两个时间是不是同一个月
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static final boolean isToMonth(Date date1, Date date2) {
		if (date1 != null && date2 != null) {
			Calendar cal1 = Calendar.getInstance();
			cal1.setTime(date1);
			Calendar cal2 = Calendar.getInstance();
			cal2.setTime(date2);

			boolean x = cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);

			boolean y = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);

			cal1 = null;
			cal2 = null;

			return x && y;
		}
		return false;
	}

}
